package designPattern;


/**
 * 单例设计模式 ----懒汉式（用到时才会去创建，线程不安全）
 * ①私有化构造函数
 * ②提供外部访问的方法
 * */
public class SingletonOne {

    public static void main(String[] args) {
        SingletionOneTest test01 = SingletionOneTest.getSingletionTest();
        SingletionOneTest test02 = SingletionOneTest.getSingletionTest();
        System.out.println(test01 == test02); //判断地址值是否相等
    }
}

class SingletionOneTest{

    public static SingletionOneTest singletion;

    private SingletionOneTest(){

    }

    //每次新建，线程不安全，代码快加锁
    public static  SingletionOneTest getSingletionTest(){
        if(singletion==null){
            synchronized(SingletionOneTest.class){
                if(singletion==null){  //线程双重检验锁
                    singletion = new SingletionOneTest();
                }
            }

        }
        return singletion;
    }


}
